package efr.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.AutorDao;
import efr.dao.EmailDao;
import efr.dao.impl.AutorDaoImpl;
import efr.dao.impl.EmailDaoImpl;
import efr.entity.Autor;
import efr.entity.Email;
import efr.util.EfrAdmUtil;

/**
 * Servlet implementation class Op4AutorSubmitServlet
 */
public class SearchEmailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(SearchEmailServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchEmailServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start SearchEmailServlet doGet()");
		String nextPage = null;
		if("Search".equals(request.getParameter("action"))){
			String email = request.getParameter("email");
			EmailDao emailDao = new EmailDaoImpl();
			List<Email> emails = emailDao.findByEmail(email);
			request.setAttribute("emails", emails);	
			nextPage = "/indexEmail.jsp";
		}
		if ("Details".equals(request.getParameter("action"))) {
			String id = request.getParameter("id");
			EmailDao emailDao = new EmailDaoImpl();
			Email email = emailDao.getEmail(Integer.valueOf(id));
			request.setAttribute("email", email);	
			request.setAttribute("action", "Modify");	
			nextPage = "/formEmail.jsp";
			
		}
		if ("Modify".equals(request.getParameter("action"))) {
			logger.debug("request.getRemoteAddr(): "+request.getRemoteAddr());
			logger.debug("request.getRemoteHost(): "+request.getRemoteHost());
			logger.debug("request.getRemotePort(): "+request.getRemotePort());
			logger.debug("request.getRemoteUser(): "+request.getRemoteUser());
			Email email = new Email();
			email.setId(Integer.valueOf(request.getParameter("id")));
			email.setNume(request.getParameter("nume"));
			email.setPrenume(request.getParameter("prenume"));
			email.setEmail(request.getParameter("email"));
			email.setObs(request.getParameter("obs")+" - unsubscribed by admin");
			email.setConfirmare(Integer.valueOf(request.getParameter("confirmare")));
			email.setCode(request.getParameter("code"));
			if (request.getParameter("ipUnsubscribe") != null && !"".equals(request.getParameter("ipUnsubscribe").trim())) {
				email.setIpUnsubscribe(request.getParameter("ipUnsubscribe"));
			} else {
				email.setIpUnsubscribe(request.getRemoteAddr()+" "+ request.getRemoteHost());
			}
			email.setDataUnsubscribe(Calendar.getInstance().getTime());
			EmailDao emailDao = new EmailDaoImpl();
			emailDao.updateEmail(email);
			nextPage = "/indexEmail.jsp";
		}
		RequestDispatcher view = request.getRequestDispatcher(nextPage);
		logger.debug("end SearchEmailServlet doGet()");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
